# -*- coding: utf-8 -*-

from odoo import models, fields, api


class PurchaseOrder(models.Model):
    _inherit = 'purchase.order'
    boat_location_id=fields.Many2one('stock.location',domain=[('usage','=','internal')])
    ship_date_start=fields.Date(string='装货开始时间')
    ship_date_end = fields.Date(string='装货结束时间')
    arrive_date=fields.Date(string=u'预计到港时间')
    reset_date=fields.Integer(string='剩余卸货时间(天)')
    partner_ref=fields.Char(related='boat_location_id.dest_name')

    ship_date=fields.Date(string=u'装货时间')
    ship_contact=fields.Char(string='货船联系人')
    ship_phone=fields.Char(string='货船电话')
    product_count=fields.Float(compute='compute_product_count',string=u'总数量')
    def compute_product_count(self):
        for r  in self:
            r.product_count=sum(line.product_uom_qty for line in r.order_line)
    @api.onchange('boat_location_id')
    def onchange_boat_location_id(self):
        if self.boat_location_id:
            self.ship_contact=self.boat_location_id.ship_contact
            self.ship_phone=self.boat_location_id.ship_phone


    def _get_destination_location(self):
        self.ensure_one()
        if self.boat_location_id:
            return self.boat_location_id.id
        if self.dest_address_id:
            return self.dest_address_id.property_stock_customer.id
        return self.picking_type_id.default_location_dest_id.id

    def button_approve(self, force=False):
        super(PurchaseOrder, self).button_approve(force=force)
        self.write({'state': 'purchase', 'date_approve': fields.Datetime.now()})
        return {}



class PurchaseOrderLine(models.Model):
    _inherit = 'purchase.order.line'
    boat_location_id=fields.Many2one('stock.location',related='order_id.boat_location_id',store=True)
    p_description = fields.Text(related='product_id.description', string='规格')
    is_water=fields.Selection([
        ('1','是'),
        ('0', '否'),
    ],string=u'是否水洗')
